{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "\n",
    "# Define the directory where the Markdown files and \"_sidebar.md\" are located\n",
    "path_docs = \"docs\"\n",
    "SIDEBAR_FILE = os.path.join(path_docs, \"_sidebar.md\")\n",
    "\n",
    "# Extract the file order from \"_sidebar.md\"\n",
    "FILES = []\n",
    "if os.path.isfile(SIDEBAR_FILE):\n",
    "    with open(SIDEBAR_FILE, \"r\", encoding=\"utf-8\") as sidebar:\n",
    "        for line in sidebar:\n",
    "            # Match the Markdown file path in each line, e.g., \"catalog.md\", \"chapter1.md\"\n",
    "            match = re.search(r'\\(([^)]+\\.md)\\)', line)\n",
    "            if match:\n",
    "                FILES.append(match.group(1))\n",
    "else:\n",
    "    print(f\"Warning: Sidebar file {SIDEBAR_FILE} does not exist.\")\n",
    "\n",
    "# Merge Markdown file content\n",
    "merged_content = \"\"\n",
    "for file in FILES:\n",
    "    file_path = os.path.join(path_docs, file)\n",
    "    if os.path.isfile(file_path):\n",
    "        with open(file_path, \"r\", encoding=\"utf-8\") as f:\n",
    "            merged_content += f.read() + \"\\n\\n\"  # Add an empty line as a separator\n",
    "    else:\n",
    "        print(f\"Warning: File {file_path} does not exist and will be skipped.\")\n",
    "\n",
    "path_releases = \"releases\"\n",
    "if not os.path.exists(path_releases):\n",
    "    os.makedirs(path_releases)\n",
    "\n",
    "# Write the merged content to a temporary file\n",
    "merged_file = f\"{path_releases}/preview.md\"\n",
    "with open(merged_file, \"w\", encoding=\"utf-8\") as f:\n",
    "    f.write(merged_content)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
